<?php

namespace App\Admin\Controllers;

use App\Models\Sport;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\MessageBag;

class SportController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('运动');
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('运动');
            $content->description('修改');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('运动');
            $content->description('新增');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Sport::class, function (Grid $grid) {

            $grid->model()->orderBy('id', 'DESC');
//            $grid->model()->where([['is_delete', '=', 0]]);
            $grid->id('ID')->sortable();

            $grid->name('名称');
            $grid->caloric('一小时消耗热量(Kcal)');
            $grid->status('状态')->switch([
                'on'  => ['value' => 1, 'text' => '正常', 'color' => 'primary'],
                'off' => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
            ]);
            $grid->created_at('创建时间');

            $grid->disableExport();

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
            });

            $grid->tools(function (Grid\Tools $tools) {
                $tools->batch(function (Grid\Tools\BatchActions $actions) {
                    $actions->disableDelete();
                });
            });
            $grid->filter(function ($filter) {
                $filter->like('name', '名称');
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Sport::class, function (Form $form) {
            $form->display('id', 'ID');
            $form->hidden('id');

            $form->text('name', '名称')->rules('required', [
                'required' => '运动名称不能为空'
            ]);

            $form->number('caloric', '运动一小时消耗热量(Kcal)')->rules('required',[
                'required' => '热量不能为空',
            ]);
            $form->switch('status', '状态')->states([
                'on'  => ['value' => 1, 'text' => '正常', 'color' => 'primary'],
                'off' => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
            ])->default(1);

            $form->saving(function (Form $form) {
                $where = [['name', '=', $form->name]];
                $id = $form->id;
                if (!empty($id)) {
                    $where[] = ['id', '<>', $id];
                }
                $is_exist = Sport::where($where)->first();
                if (!empty($is_exist)) {
                    $error = new MessageBag([
                        'title' => '错误',
                        'message' => '运动名称已存在',
                    ]);

                    return back()->with(compact('error'));
                }
            });
        });
    }
}
